package demo1;

public class Tree {
    //创建二叉树
    public TreeNode creatTree() {
        TreeNode root = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');

        root.left = B;
        root.right = C;
        B.left = D;
        B.right = E;
        C.left = F;

        return root;
    }

    public static int i;
    public TreeNode creatTree(String str){
        TreeNode root = null;
        if(str.charAt(i) == '#') {
            i++;
            return null;
        }

        root = new TreeNode(str.charAt(i));
        i++;
        root.left = creatTree(str);
        root.right = creatTree(str);

        return root;
    }

    public void preOreder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.println(root.val + " ");
        preOreder(root.left);
        preOreder(root.right);
    }

    public void inOreder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOreder(root.left);
        System.out.println(root.val + " ");
        inOreder(root.right);
    }

    public void postOreder(TreeNode root) {
        if(root == null) {
            return;
        }
        postOreder(root.left);
        postOreder(root.right);
        System.out.println(root.val + " ");
    }

    public int nodes;
    public void size(TreeNode root) {
        if(root == null) {
            return;
        }
        nodes++;
        size(root.left);
        size(root.right);
    }

    public int size2(TreeNode root) {
        if(root == null) {
            return 0;
        }

        return size2(root.left) + size2(root.right) + 1;
    }

    public int leafNodes;
    public void getLeafNodes(TreeNode root) {
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafNodes++;
        }
        getLeafNodes(root.left);
        getLeafNodes(root.right);
    }
}
